import 'package:flutter/cupertino.dart';
import 'package:flutter_learn_vi/src/bean/bean_app_setting.dart';
import 'package:flutter_learn_vi/src/bean/bean_common_config.dart';
import 'package:flutter_learn_vi/src/bean/bean_exam_test_count.dart';
import 'package:flutter_learn_vi/src/bean/bean_login.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/bean/bean_setting.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/sp_key.dart';
import 'package:flutter_learn_vi/src/page/home/new/v2_home_main_page.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/sp_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/7/24.
///
/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/9/25.
///
/// gongzhonghao biglead
/// https://study.163.com/instructor/1021406098.htm
/// https://blog.csdn.net/zl18603543572
/// https://www.toutiao.com/c/user/token/MS4wLjABAAAAYMrKikomuQJ4d-cPaeBqtAK2cQY697Pv9xIyyDhtwIM/

///lib/src/common/user_helper.dart
///用户信息辅助操作类
class UserHelper {
  // 私有构造函数
  UserHelper._() {
    // 具体初始化代码
  }

  ///获取单例对象
  static UserHelper getInstance = UserHelper._();

  //是否显示我的积分记录
  List<CommonConfigBean> configList;

  ///用户基本信息模型
  UserBean _userBean;
  MineBean _mineBean;
  LoginBean _loginBean;
  SettingBean settingBean;

  num userLongitude;
  num userLatitude;
  String userAddress;
  String currentWeatherInfo;
  String adCode;

  AppSettingBean appSettingBean;

  ExamTestCountConfigBean examTestCountConfigBean;

  //启动图片
  List<String> _startUrlList;

  String _string;

  List<Function> _list = [];

  ///上次人脸识别的时间
  DateTime lastFaceRecognitionTime;

  ///人脸识别计时
  int faceRecognitionNumber = 0;
  void addLierner(Function(String value) ca) {
    _list.add(ca);
  }

  void set string(String str) {
    _list.forEach((element) {
      element(str);
    });
    _string = str;
  }

  ///题库ID
  String _questionUid;
  List<MineQuestionBean> _questionList;
  MineQuestionBean _selectMineQuestionBean;

  ///获取 UserBean
  UserBean get userBean => _userBean;

  bool isShowFaceCheck() {
    /// 人脸识别开关 默认0开启 ，1关闭
    int userFaceOpen = _userBean.userFaceOpen;
    if (userFaceOpen == 0) {
      return true;
    }
    return false;
  }

  MineQuestionBean get selectMineQuestionBean => _selectMineQuestionBean;

  MineBean get mineBean => _mineBean;

  List<String> get startUrlList => _startUrlList;

  LoginBean get loginBean => _loginBean;

  String get questionUid => _questionUid;

  List<MineQuestionBean> get questionList => _questionList;

  void set selectMineQuestionBean(MineQuestionBean bean) {
    _selectMineQuestionBean = bean;
    questionUid = _selectMineQuestionBean.questionUid;
  }

  set startUrlList(List<String> list) {
    _startUrlList = list;
    SPUtil.saveStringList(
        spStartPicListKey + "${_userBean.userId}", _startUrlList);
  }

  set questionList(List<MineQuestionBean> questionList) {
    LogUtils.e("获取题库信息 1");
    _questionList = questionList;
    _questionList.forEach((element) {
      if (element.isDefault != null && element.isDefault) {
        _selectMineQuestionBean = element;
        _questionUid = _selectMineQuestionBean.questionUid;
      }
    });
    questionUid = _questionUid;
  }

  set questionUid(String uid) {
    _questionUid = uid;
    SPUtil.save(spQuestionKey + "${_userBean.userId}", _questionUid);
  }

  ///userBean的设置方法
  void setUserBean(UserBean bean) {
    _userBean = bean;

    ///缓存用户信息
    SPUtil.saveObject(spUserBeanKey, bean);
  }

  void removeUserBean() {
    _userBean = null;

    ///缓存用户信息
    SPUtil.remove(spUserBeanKey);
  }

  set loginBean(LoginBean bean) {
    _loginBean = bean;
    SPUtil.saveObject(spLoginBeanKey, _loginBean);
  }

  set mineBean(MineBean mineBean) {
    _mineBean = mineBean;
    SPUtil.saveObject(spMineBeanKey + "${_userBean.userId}", mineBean);
  }

  ///判断用户是否登录的便捷方法
  bool get userIsLogin => userBean == null ? false : true;

  ///是否同同意隐私与用户协议
  bool _userProtocol = true;

  bool get userProtocol => _userProtocol;

  set userProtocol(bool flag) {
    _userProtocol = flag;

    ///保存同意的标识
    SPUtil.save(spUserProtocolKey, flag);
  }

  ///判断用户是否同意用户协议便捷方法
  bool get isUserProtocol => _userProtocol == null ? false : _userProtocol;

  ///用来初始化用户信息的缓存数据
  Future<bool> init() async {
    ///加载缓存数据
    Map<String, dynamic> map = await SPUtil.getObject(spUserBeanKey);
    if (map != null) {
      ///解析缓存数据
      _userBean = UserBean.fromJson(map);

      Map<String, dynamic> map2 =
          await SPUtil.getObject(spMineBeanKey + "${_userBean.userId}");
      if (map2 != null) {
        _mineBean = MineBean.fromJson(map2);
      }

      Map<String, dynamic> map3 = await SPUtil.getObject(spLoginBeanKey);
      if (map3 != null) {
        _loginBean = LoginBean.fromCacheJson(map3);
      }
      _questionUid = SPUtil.getString(spQuestionKey + "${_userBean.userId}");
      _startUrlList =
          SPUtil.getStringList(spStartPicListKey + "${_userBean.userId}");
    }

    return Future.value(true);
  }

  //退出登录 清除数据
  void exitLogin() {
    _userBean = null;
    SPUtil.remove(spUserBeanKey);
    SPUtil.remove(spMineBeanKey);
  }

  void goHomePage(BuildContext context) {
    //矿灯学院不校验登录方式
    NavigatorUtils.openPageByFade(context, HomeMainV2Page(), isReplace: true);
    // int isCheck = UserHelper.getInstance.userBean.isCheck;
    // if (isCheck == 1) {
    //   NavigatorUtils.openPageByFade(context, HomeMainPage(), isReplace: true);
    // } else if (isCheck == 2) {
    //   //伯方
    //   NavigatorUtils.openPageByFade(context, HomeMainV3Page(), isReplace: true);
    // } else {
    //   if (Application.getInstance.isBoFang()) {
    //         NavigatorUtils.openPageByFade(context, HomeMainV4Page(),
    //             isReplace: true);
    //       } else {
    //         NavigatorUtils.openPageByFade(context, HomeMainV2Page(),
    //             isReplace: true);
    //       }
    // }
  }
}
